package com.tdk.bts.dao;

import com.tdk.bts.entity.Film;
import com.tdk.bts.util.DruidConnectionUtil;

import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FilmServiceDao {
    private static Connection conn = null;
    static {
                if (conn == null) {
                    try {
                        conn = DruidConnectionUtil.getConnection();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
    }
//  查询影片
    public List<Film> findFilms(int page) throws SQLException {
            List<Film> list = new ArrayList<>();
            Film film = null;
            int m = (page - 1) * 8;
            String sql = "select * from t_film ORDER BY publishDate DESC limit ?,?";
            PreparedStatement preSt = conn.prepareStatement(sql);
            preSt.setInt(1, m);
            preSt.setInt(2, 8);
            ResultSet rs = preSt.executeQuery();
            while (rs.next()) {
                film = new Film();
                int id = rs.getInt("id");
                film.setId(id);
                String name = rs.getString("name");
                film.setName(name);
                String info = rs.getString("info");
                film.setInfo(info);
                String picUrl = rs.getString("picUrl");
                film.setPicUrl(picUrl);
                Date publicDate = rs.getDate("publishDate");
                film.setPublishDate(publicDate);
                list.add(film);
            }
            DruidConnectionUtil.closeAll(preSt, rs);
            return list;
        }

//  添加影片
    public void addFilms(List<Film> films) throws SQLException{
        String sql = "INSERT into t_film(name,info,picUrl,publishDate) values(?,?,?,?)";
        PreparedStatement preSt = conn.prepareStatement(sql);
        for(Film film : films){
            preSt.setString(1,film.getName());
            preSt.setString(2, film.getInfo());
            preSt.setString(3,film.getPicUrl());
            preSt.setDate(4,new java.sql.Date(film.getPublishDate().getTime()));
            preSt.addBatch();
        }
        preSt.executeBatch();
        DruidConnectionUtil.closeAll(preSt,null);
    }

    private static List<Integer> getIds(String ids) {
        String[] str = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (String id : str){
            int intId = Integer.parseInt(id);
            list.add(intId);
        }
        return list;
    }

    //获取售出影票数量
    private static int getCount(String ids) throws SQLException {
        List<Integer> list = FilmServiceDao.getIds(ids);
        int num = 0;
        PreparedStatement preSt = null;
        ResultSet rs = null;
        for(int i = 0; i<list.size();i++){
            String sql = "SELECT COUNT(status) FROM t_film as film,t_film_screen as screen," +
                    "t_book_info as bookInfo " +
                    "WHERE film.id=screen.film_id AND screen.id=bookInfo.screen_id " +
                    "AND film.id=" + list.get(i);
            preSt = conn.prepareStatement(sql);
            rs = preSt.executeQuery(sql);
            while (rs.next()){
                num = rs.getInt("count(status)");
            }

        }
        DruidConnectionUtil.closeAll(preSt,rs);
        return num;
    }

    //删除上档电影
    public void removeFilms(String ids) throws SQLException{
        List<Integer> list = FilmServiceDao.getIds(ids);
        //获取售出影票数量
        int count = FilmServiceDao.getCount(ids);
        PreparedStatement preSt = null;
       if(count > 0){
            //删除失败提示信息
            JOptionPane.showMessageDialog(null,"此电影已开启售票，无法删除档期","提示信息" , JOptionPane.INFORMATION_MESSAGE);
        }else{
                for (int i = 0; i< list.size();i++){
                    String sql = "delete from t_film where id=" + list.get(i);
                    preSt = conn.prepareStatement(sql);
                    preSt.execute();
                }
            //删除成功提示信息
            JOptionPane.showMessageDialog(null,"删除成功","提示信息",JOptionPane.INFORMATION_MESSAGE);
        }
       DruidConnectionUtil.closeAll(preSt,null);
    }

    public int FilmTotalCount() throws SQLException {
        String sql = "select COUNT(id) from t_film";
        PreparedStatement preSt = conn.prepareStatement(sql);
        ResultSet rs = preSt.executeQuery();
        int num = 0;
        if (rs.next()) {
            num = rs.getInt(1);
        }
        DruidConnectionUtil.closeAll( preSt, rs);
        return num;
    }
}

